package com.ctbri.JWLetter.dao.es.impl;

import com.ctbri.JWLetter.bean.Letter;
import com.ctbri.JWLetter.dao.es.LetterDao;
import com.ctbri.JWLetter.frame.BaseDao;

import org.apache.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 信件数据层实现
 * 
 * @author Hogan
 * 
 */
@Repository("letterDao")
public class LetterDaoImpl extends BaseDao implements LetterDao {

	private Logger log = Logger.getLogger(getClass());

	@Override
	public List<Letter> findLettersByMultiParams(int startCount, Integer pageSize, String key, Object... values) {
		return super.findLettersByMultiParams(startCount, pageSize, key, values);
	}

	@Override
	public long findCountByMultiParams(String key, Object... values) {
		return super.findCountByMultiParams(key, values);
	}

	@Override
	public long findCountByOneParam(String key, Object value) {
		return super.findCountByOneParam(key, value);
	}

	@Override
	public List<Letter> findLettersByOneParam(int startCount, Integer pageSize, String key, Object value) {
		return super.findLettersByOneParam(startCount, pageSize, key, value);
	}

	@Override
	public List<Letter> findLettersByPage(int startCount, int letterSize) {
		TransportClient client = eSDataPooledConnection.getCurrentConnecton();
		SearchResponse sr = client.prepareSearch("articles").setFrom(startCount).setSize(letterSize).get();
		Iterator<SearchHit> it = sr.getHits().iterator();
		List<Letter> result = new ArrayList<Letter>();
		while (it.hasNext()) {
			Map<String, Object> map = ((SearchHit) it.next()).getSource();
			Letter letter = new Letter();
			letter.setLetterId(get("id", Integer.class, map));
			letter.setAuthor(get("author", String.class, map));
			letter.setCategory(get("categoryname", Integer.class, map));
			letter.setStatus(get("readornot", Integer.class, map));
			letter.setTitle(get("title", String.class, map));
			letter.setSubmitDate(get("submiteDatetime", Date.class, map));
			result.add(letter);
		}
		eSDataPooledConnection.releaseConn(client);
		return result;
	}

	@Override
	public long findLetterCount() {
		TransportClient client = eSDataPooledConnection.getCurrentConnecton();
		long count = client.prepareSearch("articles").get().getHits().getTotalHits();
		eSDataPooledConnection.releaseConn(client);
		return count;
	}

	@Override
	public List<Letter> findLettersByIds(List<Integer> letterIds) {
		TransportClient client = eSDataPooledConnection.getCurrentConnecton();
		SearchResponse sr = client.prepareSearch("articles").setQuery(QueryBuilders.termsQuery("id", letterIds)).get();
		Iterator<SearchHit> it = sr.getHits().iterator();
		List<Letter> result = new ArrayList<Letter>();
		while (it.hasNext()) {
			Map<String, Object> map = ((SearchHit) it.next()).getSource();
			Letter letter = new Letter();
			letter.setLetterId(get("id", Integer.class, map));
			letter.setAuthor(get("author", String.class, map));
			letter.setCategory(get("categoryname", Integer.class, map));
			letter.setStatus(get("readornot", Integer.class, map));
			letter.setTitle(get("title", String.class, map));
			letter.setSubmitDate(get("submiteDatetime", Date.class, map));
			result.add(letter);
		}
		eSDataPooledConnection.releaseConn(client);
		return result;
	}

	@Override
	public Letter findLetterById(Integer letterId) {
		TransportClient client = eSDataPooledConnection.getCurrentConnecton();
		SearchResponse sr = client.prepareSearch("articles").setQuery(QueryBuilders.termQuery("id", letterId)).get();
		Iterator<SearchHit> it = sr.getHits().iterator();
		Letter letter = null;
		while (it.hasNext()) {
			Map<String, Object> map = ((SearchHit) it.next()).getSource();
			letter = new Letter();
			letter.setLetterId(get("id", Integer.class, map));
			letter.setAuthor(get("author", String.class, map));
			letter.setCategory(get("categoryname", Integer.class, map));
			letter.setStatus(get("readornot", Integer.class, map));
			letter.setTitle(get("title", String.class, map));
			letter.setSubmitDate(get("submiteDatetime", Date.class, map));
			letter.setContent(get("contents", String.class, map));
			break;
		}
		eSDataPooledConnection.releaseConn(client);
		return letter;
	}

	@Override
	public Integer modifyLetterStatus(Integer letterId, Integer status) {
		TransportClient client = eSDataPooledConnection.getCurrentConnecton();
		UpdateRequest request = new UpdateRequest("articles", "article", letterId + "");
		request.doc(new HashMap<String, Integer>().put("readornot", status));
		try {
			UpdateResponse ur = client.update(request).get();
			ur.status().getStatus();
		} catch (Exception e) {
			log.error(e);
		} finally {
			eSDataPooledConnection.releaseConn(client);
		}
		return null;
	}

}
